package cn.dnui_zjq823.dnuifood_16110100823.detail;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_zjq823.dnuifood_16110100823.R;
import cn.dnui_zjq823.dnuifood_16110100823.adapter.CommentAdapter;
import cn.dnui_zjq823.dnuifood_16110100823.adapter.FoodAdapter;
import cn.dnui_zjq823.dnuifood_16110100823.bean.CommentBean;
import cn.dnui_zjq823.dnuifood_16110100823.bean.ResultBean;
import cn.dnui_zjq823.dnuifood_16110100823.listener.onRetrofitListener;
import cn.dnui_zjq823.dnuifood_16110100823.model.CollectModel;
import cn.dnui_zjq823.dnuifood_16110100823.model.CommentModel;
import cn.dnui_zjq823.dnuifood_16110100823.model.ShopModel;

/**
 * Created by dell on 2018/11/19.
 */

public class FoodDetail extends AppCompatActivity implements onRetrofitListener {
    private Context context;
    private String foodname,foodprice,foodintro,foodrecommand,foodpic;
    private TextView fd_name,fd_price,fd_intro,fd_recommand;
    private ImageView fd_pic;
    private String url="http://172.24.10.175:8080/foodService/";
    SharedPreferences sp;
    private int userid,food_id;
    private MenuItem item;
    private CollectModel collectmodel;
    private CommentModel commentmodel;
    private String collected,success;
    private List<CommentBean> list;
    private CommentAdapter commentAdapter;
    private RecyclerView recycler_comment;
    private RecyclerView.LayoutManager layoutManager;
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_food);
        context=getApplicationContext();
        initRecycleview();
        fooddate();
        food_id=Integer.parseInt(getIntent().getStringExtra("foodid"));
        commentmodel=new CommentModel();
        commentmodel.getAllCommentsByFood(food_id,this);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menucollect,menu);
        item=menu.findItem(R.id.menucollect);
        collectmodel=new CollectModel();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        String uid=sp.getString("userid",null);
        userid=Integer.parseInt(uid);
        collectmodel.isCollected(userid,food_id,1,this);
        return true;
    }
    private void fooddate(){
        foodname=getIntent().getStringExtra("foodname");
        foodprice=getIntent().getStringExtra("foodprice");
        foodintro=getIntent().getStringExtra("foodintro");
        foodrecommand=getIntent().getStringExtra("foodrecommand");
        foodpic=getIntent().getStringExtra("foodpic");
        fd_name=findViewById(R.id.fd_name);
        fd_price=findViewById(R.id.fd_price);
        fd_recommand=findViewById(R.id.fd_recommand);
        fd_intro=findViewById(R.id.fd_intro);
        fd_pic=findViewById(R.id.fd_pic);
        if (foodrecommand==null) {
            foodrecommand="";
        }
        else if (foodrecommand.equals("1")){
            foodrecommand="推荐";
        }
        else {
            foodrecommand = "一般";
        }
        Picasso.with(context).load(url+foodpic).fit().placeholder(R.mipmap.ic_launcher).into(fd_pic);
        fd_name.setText("菜名："+foodname);
        fd_price.setText("价格："+foodprice);
        fd_recommand.setText("推荐："+foodrecommand);
        fd_intro.setText("介绍："+foodintro);
    }
    @Override
    public void onSuccess(Object object, int flag) {
        if(flag==1){
            ResultBean resultBean;
            resultBean=(ResultBean)object;
            collected=resultBean.getCollected();
            if (collected.equals("1")){
                item.setTitle("取消收藏");
            }
            if (collected.equals("0")){
                item.setTitle("收藏");
            }
        }
        if(flag==4){
            ResultBean resultBean;
            resultBean=(ResultBean)object;
            success=resultBean.getSuccess();
            if (success.equals("1")){
                collectmodel.isCollected(userid,food_id,1,this);
            }
        }
        if (flag==5){
            list=(List)object;
            commentAdapter = new CommentAdapter(this);
            commentAdapter.setList(list);
            recycler_comment.setAdapter(commentAdapter);
        }
    }
    private void initRecycleview(){
        recycler_comment=findViewById(R.id.recycler_comment);
        layoutManager=new LinearLayoutManager(this);
        recycler_comment.setLayoutManager(layoutManager);
    }
    @Override
    public void onFalure(String msg) {
        Toast.makeText(this, "失败："+msg, Toast.LENGTH_SHORT).show();
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.menucollect:
                collectmodel.userCollectFood(userid,food_id,this);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
